package com.atguigu.exercise1;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 *
 *      创建该类的  5  个对象，并把这些对象放入  TreeSet  集合中（下一章：
 *          TreeSet  需使用泛型来定义）
 *          分别按以下两种方式对集合中的元素进行排序，并遍历输出：
 *
 *          1).  使Employee  实现  Comparable  接口，并按  name  排序
 *          2).  创建  TreeSet  时传入  Comparator 对象，按生日日期的先后排序。
 *
 *
 * @author : leimingchao
 * @date Date : 2021年10月06日 16:45
 */
public class EmployeeTest {

    /**
     * 问题二：按照生日日期的先后顺序
     */
    @Test
    public void test2() {

        TreeSet set = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Employee && o2 instanceof Employee) {
                    Employee employee1 = (Employee) o1;
                    Employee employee2 = (Employee) o2;

                    MyDate birthday1 = employee1.getBirthday();
                    MyDate birthday2 = employee2.getBirthday();
                    //方式一：
                    //比较年
                    // int minusYear = birthday1.getYear() - birthday2.getYear();
                    // if (minusYear != 0) {
                    //     return minusYear;
                    // }
                    //比较月
                    // int minusMonth = birthday1.getMonth() - birthday2.getMonth();
                    // if (minusMonth != 0) {
                    //     return minusMonth;
                    // }
                    //比较日
                    // return birthday1.getDay() - birthday2.getDay();

                    //方式二：
                    return birthday1.compareTo(birthday2);
                }
                // return 0;
                throw new RuntimeException("传入的数据类型不一致");
            }
        });

        Employee employee = new Employee("liudehua",55,new MyDate(1965,5,4));
        Employee employee2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4));
        Employee employee3 = new Employee("guofucheng",44,new MyDate(1987,5,4));
        Employee employee4 = new Employee("liming",51,new MyDate(1954,8,12));
        Employee employee5 = new Employee("liangchaowei",21,new MyDate(1978,12,4));

        set.add(employee);
        set.add(employee2);
        set.add(employee3);
        set.add(employee4);
        set.add(employee5);

        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }

    /**
     * 问题一：使用自然排序
     */
    @Test
    public void test1() {
        TreeSet set = new TreeSet();

        Employee employee = new Employee("liudehua",55,new MyDate(1965,5,4));
        Employee employee2 = new Employee("zhangxueyou",43,new MyDate(1987,5,4));
        Employee employee3 = new Employee("guofucheng",44,new MyDate(1987,5,9));
        Employee employee4 = new Employee("liming",51,new MyDate(1954,8,12));
        Employee employee5 = new Employee("liangchaowei",21,new MyDate(1978,12,4));

        set.add(employee);
        set.add(employee2);
        set.add(employee3);
        set.add(employee4);
        set.add(employee5);

        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
